<template>
  <div class="container">
    <el-card>
      <el-row>
        <el-col :span="22">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item label="关键字">
              <el-input v-model="requestParams.keyword" size="small"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="requestParams.state" placeholder="请选择" size="small">
                <el-option label="启用" :value="1"></el-option>
                <el-option label="禁用" :value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="small" @click="clear()" class="clear">清除</el-button>
              <el-button type="primary" size="small" @click="search()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="2">
          <el-button type="success" icon="el-icon-edit" size="small" @click="addSkill()">新增技巧</el-button>
        </el-col>
      </el-row>

      <el-alert :title="`数据一共${total}条`" type="info" show-icon :closable="false"></el-alert>

      <el-table :data="tableData">
        <el-table-column label="序号" width="80" type="index"></el-table-column>
        <el-table-column label="文章标题" width="400">
          <template slot-scope="scope">
            {{ scope.row.title }}
            <a
              href="#"
              class="el-icon-film"
              v-if="scope.row.videoURL"
              @click.prevent="playVideo(scope.row.videoURL)"
            ></a>
          </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="visits"></el-table-column>
        <el-table-column label="录入人" prop="username"></el-table-column>
        <el-table-column label="录入时间">
          <template slot-scope="scope">{{ scope.row.createTime|parseTimeByString }}</template>
        </el-table-column>
        <el-table-column label="状态" width="100px">
          <template slot-scope="scope">{{ scope.row.state === 1 ? '已启用': '已禁用' }}</template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="text" @click="preview(scope.row)">预览</el-button>
            <el-button
              type="text"
              @click="isState(scope.row)"
            >{{ scope.row.state === 1 ? '禁用': '启用' }}</el-button>
            <el-button type="text" @click="addSkill(scope.row)" :disabled="scope.row.state===1">修改</el-button>
            <el-button type="text" @click="delArticle(scope.row)" :disabled="scope.row.state===1">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        class="page"
        background
        layout="prev, pager, next,sizes,jumper"
        :total="total"
        :page-sizes="[5,10,20,50]"
        :page-size="requestParams.pageSize"
        :current-page="requestParams.page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </el-card>
    <preview-article ref="previewArticle" :data="thearticle"></preview-article>
    <add-article ref="addArticle" :data="thearticle" @changeArticleList="changeArticleList()"></add-article>
    <div class="shipin" v-if="videoURL">
      <div class="stopPlayVideo" @click="stopVideo()">
        <span class="el-icon-close"></span>
      </div>
      <div class="player">
        <video :src="videoURL" ref="video"></video>
      </div>
    </div>
		<drag></drag>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles'
import previewArticle from '../components/articles-preview'
import addArticle from '../components/articles-add'
export default {
  data () {
    return {
      tableData: [],
      total: 0,
      thearticle: {},
      videoURL: null,
      requestParams: {
        keyword: null,
        page: 1,
        pagesize: 10,
        state: null
      }
    }
  },
  components: {
    previewArticle,
    addArticle
  },
  created () {
    this.getData()
  },
  methods: {
    // 拿到文章的列表数据
    async getData () {
      const { data } = await list(this.requestParams)
      this.tableData = data.items
      this.total = data.counts
    },
    // 搜索
    search () {
      this.requestParams.page = 1
      this.getData()
    },
    // 清除
    clear () {
      this.requestParams = {
        keyword: null,
        page: 1,
        pagesize: 10,
        state: null
      }
    },
    // 预览
    preview (data) {
      this.thearticle = data
      this.$nextTick(() => {
        this.$refs.previewArticle.showArticle()
      })
    },
    // 修改状态
    async isState (data) {
      await changeState({
        id: data.id,
        state: data.state === 1 ? 0 : 1
      })
      this.$message.success('修改状态成功')
      data.state = data.state === 1 ? 0 : 1
    },
    // 删除文章
    async delArticle (data) {
      await this.$confirm('确定删除该文章吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(data)
      this.$message.success('成功删除文章')
      this.getData()
    },
    // 新增技巧
    addSkill (data = {}) {
      this.thearticle = data
      this.$nextTick(() => {
        this.$refs.addArticle.showAddArticle()
      })
    },
    // 重新渲染文章的  列表
    changeArticleList () {
      if (!this.thearticle.id) {
        this.requestParams.page = 1
      }
      this.getData()
    },
    // 更改每一页的显示数目
    handleSizeChange (val) {
      this.requestParams.page = 1
      this.requestParams.pagesize = val
      this.getData()
    },
    // 改变当前页码
    handleCurrentChange (val) {
      this.requestParams.page = val
      this.getData()
    },
    // 播放视频
    playVideo (val) {
      this.videoURL = val
      this.$nextTick(() => {
        this.$refs.video.play()
      })
    },
    // 关闭视频
    stopVideo () {
      this.$refs.video.pause()
      this.videoURL = null
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 10px;
  .clear {
    background-color: white;
     color: #000;
     border: 1px solid silver;
  }
  .el-icon-film {
    color: blue;
    font-size: 20px;
  }
  .page {
    text-align: right;
    margin-top: 15px;
  }
  .shipin {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    overflow: hidden;
    z-index: 9999;
    .stopPlayVideo {
      width: 50px;
      height: 50px;
      background: rgba(0, 0, 0, 0.4);
      box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
      border-radius: 50%;
      position: absolute;
      top: 30px;
      left: 50%;
      transform: translate(-50%, 0);
      text-align: center;
      line-height: 50px;
      color: white;
      font-size: 20px;
    }
    .player {
      width: 800px;
      height: 600px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      video {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>
